import os
from datetime import timedelta
from dotenv import load_dotenv

load_dotenv()

class Config:
    # Flask基础配置
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev-secret-key-change-in-production'
    DEBUG = os.environ.get('DEBUG', 'False').lower() == 'true'
    
    # 数据库配置
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///aidataserver.db'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    
    # Redis配置
    REDIS_URL = os.environ.get('REDIS_URL')
    REDIS_HOST = os.environ.get('REDIS_HOST', '192.168.1.25')
    REDIS_PORT = int(os.environ.get('REDIS_PORT', 6379))
    REDIS_PASSWORD = os.environ.get('REDIS_PASSWORD')
    REDIS_DB = int(os.environ.get('REDIS_DB', 0))
    
    # 如果没有提供REDIS_URL，则根据其他参数构建
    if not REDIS_URL:
        if REDIS_PASSWORD:
            REDIS_URL = f'redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}'
        else:
            REDIS_URL = f'redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}'
    
    # JWT配置
    JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY') or 'jwt-secret-key'
    JWT_ACCESS_TOKEN_EXPIRES = timedelta(seconds=int(os.environ.get('JWT_ACCESS_TOKEN_EXPIRES', 3600)))
    JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=30)
    
    # 微信公众号配置
    WECHAT_APP_ID = os.environ.get('WECHAT_APP_ID')
    WECHAT_APP_SECRET = os.environ.get('WECHAT_APP_SECRET')
    WECHAT_TOKEN = os.environ.get('WECHAT_TOKEN')
    
    # 腾讯云短信服务配置
    TENCENT_SMS_SECRET_ID = os.environ.get('TENCENT_SMS_SECRET_ID')
    TENCENT_SMS_SECRET_KEY = os.environ.get('TENCENT_SMS_SECRET_KEY')
    TENCENT_SMS_APP_ID = os.environ.get('TENCENT_SMS_APP_ID')
    TENCENT_SMS_SIGN_NAME = os.environ.get('TENCENT_SMS_SIGN_NAME')
    TENCENT_SMS_TEMPLATE_ID = os.environ.get('TENCENT_SMS_TEMPLATE_ID')
    TENCENT_SMS_REGION = os.environ.get('TENCENT_SMS_REGION', 'ap-beijing')
    
    # 加密配置
    ENCRYPTION_KEY = os.environ.get('ENCRYPTION_KEY') or b'default-32-byte-key-change-this!'
    
    # CORS配置
    CORS_ORIGINS = ["http://localhost:3000", "http://127.0.0.1:3000"]